#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include <signal.h>
#include<sys/time.h>
#include<linux/posix_types.h>

/*
I catch the SIGALRM
I catch the SIGALRM
^CI catch the SIGINT
I catch the SIGALRM
I catch the SIGALRM
^CI catch the SIGINT
I catch the SIGALRM
^CI catch the SIGINT
I catch the SIGALRM
I catch the SIGALRM
^\退出 (核心已转储)

 */

typedef void (*sighandler_t)(int);

sighandler_t oldact;

void handler(int sig){
	if(sig == SIGINT){
		printf("I catch the SIGINT\n");
	}else if(sig == SIGALRM){
		printf("I catch the SIGALRM\n");
	}
}

int main(){
	//初始化sigaction
	struct sigaction act;
	act.sa_handler = handler;
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);

	//初始化定时信号
	struct itimerval timevalue;
	//每间隔1秒发一次定时信号
	timevalue.it_interval.tv_sec = 1;
	timevalue.it_interval.tv_usec = 0;
	//等待5秒开始发定时信号
	timevalue.it_value.tv_sec = 5;
	timevalue.it_value.tv_usec = 0;
	//开始周期发送定时信号
	setitimer(ITIMER_REAL, &timevalue, NULL);

	
	//信号捕捉1：CTRL + C信号的行为 由杀掉进程改成 打印一行话 
	sigaction(SIGINT, &act, NULL);
	//信号捕捉2：ALARM 由杀掉进程改成 打印一行话
	sigaction(SIGALRM, &act, NULL);


	while(1){
		sleep(1);
	}
}
